<?phpinclude_once APPPATH."libraries/core/Crmmodel.php";class Aircompanies_model extends Crmmodel{
	public function __construct(){		parent::__construct();	}
		public function get_ds(){		$this->db->select('SQL_CALC_FOUND_ROWS _aircompanies.rid as rid, _aircompanies.name as name,  							_aircompanies.code as code, _aircompanies.iata as iata, _aircompanies.archive,							_aircompanies.owner_users_rid,							DATE_FORMAT(_aircompanies.modifyDT, \'%d.%m.%Y\') as modifyDT', False);		$this->db->from('_aircompanies');		if($searchRule = element('like', $this->ci->get_session('searchrule'), null)) $this->db->like($searchRule);		if($searchRule = element('where', $this->ci->get_session('searchrule'), null)) $this->db->where($searchRule);		if($searchRule = element('having', $this->ci->get_session('searchrule'), null)) $this->db->having($searchRule);		if($sort = $this->ci->get_session('sort'))	$this->db->orderby($sort['c'], $sort['r']);		$this->db->limit($this->ci->config->item('crm_grid_limit'), element('p', $this->ci->a_uri_assoc, null));		$query = $this->db_get('_aircompanies');		return $query->num_rows()?$query->result():array();	}
		public function get_edit($rid){		$this->db->select('_aircompanies.rid as rid, _aircompanies.name as name,  							_aircompanies.code as code, _aircompanies.iata as iata,							_aircompanies.owner_users_rid,							_aircompanies.descr as descr, _aircompanies.archive as archive', False);		$this->db->from('_aircompanies');		$this->db->where(array('_aircompanies.rid'=>$rid));		$query = $this->db_get('_aircompanies');		return $query->num_rows()?$query->row():False;	}
	
	public function create_record(){		$ins_arr = array('name'=>$this->ci->input->post('name'),							'code'=>$this->ci->input->post('code'),							'iata'=>$this->ci->input->post('iata'),							'descr'=>$this->ci->input->post('descr'),							'archive'=>$this->ci->input->post('archive'),							'owner_users_rid'=>get_curr_urid(),							'modifier_users_rid'=>get_curr_urid());		$this->db->set('createDT', 'now()', False);		$this->db->set('modifyDT', 'now()', False);		$this->db->trans_begin();		$this->db->insert('_aircompanies', $ins_arr);		$insRid = $this->db->insert_id();		if ($this->db->trans_status() === FALSE){    		$this->db->trans_rollback();    		return False;		}else{    		$this->db->trans_commit();    		return $insRid;		}			}
	
	public function update_record(){		$update_arr = array('name'=>$this->ci->input->post('name'),							'code'=>$this->ci->input->post('code'),							'iata'=>$this->ci->input->post('iata'),							'descr'=>$this->ci->input->post('descr'),							'archive'=>$this->ci->input->post('archive'),							'modifier_users_rid'=>get_curr_urid());		$this->db->set('modifyDT', 'now()', False);		$this->db->trans_begin();		$this->db->update('_aircompanies', $update_arr, array('rid'=>$this->ci->input->post('rid')));		if ($this->db->trans_status() === FALSE){    		$this->db->trans_rollback();    		return False;		}else{    		$this->db->trans_commit();    		return True;		}			}
		public function remove_items(){		$this->db->trans_begin();		foreach($this->ci->input->post('row') as $rid){			$this->db->delete('_aircompanies', array('rid'=>$rid));			}		if ($this->db->trans_status() === FALSE){    		$this->db->trans_rollback();    		return False;		}else{    		$this->db->trans_commit();    		return True;		}			}
	
	public function check_unique($val, $type='name', $rid=null){		$this->db->select('count(*) as quan');		$this->db->from('_aircompanies');		if($type=='name') $this->db->where(array('name'=>$val));		else $this->db->where(array('code'=>$val));		if($rid) $this->db->where(array('rid != '=>$rid));		$query = $this->db->get();		return $query->num_rows()?$query->row()->quan:0;	}
	
	public function move_record(){		$update_doc = array('owner_users_rid'=>get_urid_byemprid($this->ci->input->post('_employeers_rid')));		$this->db->set('modifyDT', 'now()', False);		$this->db->trans_begin();		$this->db->update('_aircompanies', $update_doc, array('_aircompanies.rid'=>$this->ci->input->post('rid')));		if ($this->db->trans_status() === FALSE){    		$this->db->trans_rollback();    		return False;		}else{    		$this->db->trans_commit();    		return $this->ci->input->post('rid');		}			}		public function get_aircompanyname_byrid($rid){		$this->db->select('name', False);		$this->db->from('_aircompanies');		$this->db->where(array('rid'=>$rid));		$this->db->order_by('name');		$query = $this->db->get();		return $query->num_rows()?$query->row()->name:null; 	}		}?>